package com.zj.open.wechat.service;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zj.open.wechat.dto.msg.Message;

@Service
public class WechatService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    MessageProcessorFactory processorFactory;

    @SuppressWarnings("unchecked")
    public void handleMsg(String openid, String body) {
        logger.info("|openid|{}|body|{}", openid, body);
        Document doc;
        try {
            doc = DocumentHelper.parseText(body);
            Message message = MessageFactory.getMessage(doc.getRootElement());
            if (message != null) {
                processorFactory.getInstance(message).handle(message);
            }
        } catch (DocumentException e) {
            logger.error("", e);
        }
    }

}
